﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DIProject.Dictionary
{
    public partial class NewGoodDiv : Form
    {
        private Int32 _returnGoods = Int32.MinValue;
/// <summary>
/// разделенный товар
/// </summary>
        public Int32 ReturnGoods
        {
            get { return _returnGoods; }
            set { _returnGoods = value; }
        }
        private Int32  _inGood;
        /// <summary>
        /// товар, который делим
        /// </summary>
        public Int32  InGood
        {
            get { return _inGood; }
            set { _inGood = value; }
        }
        private Int32  _retPack;
        /// <summary>
        /// Количество в разделенной упаковке
        /// </summary>
        public Int32  RetPack
        {
            get 
            {
                string str = textBox1.Text;
                return Convert.ToInt32(str);               
            }
            set 
            { 
                textBox1.Text = value.ToString(); 
            }
        }

        /// <summary>
        /// наименование товара который делим
        /// </summary>
        public string GoodName
        {
            get
            {
                return lblDivGoods.Text;
            }
            set
            {
                lblDivGoods.Text = value;
            }
        }
        DataSets.NewDistTableAdapters.GoodTableAdapter adapter = new DIProject.DataSets.NewDistTableAdapters.GoodTableAdapter();
        DataSets.NewDist.GoodDataTable _data;
        DataSets.NewDist.GoodRow _row
        {
            get
            {
                if (_data == null)
                    _data = adapter.GetData(InGood);
                if (_data == null)
                    throw new Util.DIException("Невозможно разделить товар");
                if(_data.Count<1)
                    throw new Util.DIException("Невозможно разделить товар");
                DataSets.NewDist.GoodRow res = _data.Rows[0] as DataSets.NewDist.GoodRow;
                return res;
            }
        }
        public NewGoodDiv(Int32 goodId)
        {
            InGood = goodId;
            InitializeComponent();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            decimal? FixedPrice = null;
            if (!_row.IsFixedPriceNull())
                FixedPrice = _row.FixedPrice;
            string ManufactureBarCode = null;
            if (!_row.IsManufactureBarCodeNull())
                ManufactureBarCode = _row.ManufactureBarCode;
            else ManufactureBarCode = ""; // NULL не прокатывает, пишем пустую строчку
            Int32 tmp =
            Data.Goods.NewGood(_row.AssortmentId, _row.ManufacturerId, _row.CountryId, 1, RetPack, null, false,
                ManufactureBarCode, FixedPrice, _row.DefekturaAggregate, txbNote.Text, DateTime.Now, FirstForm.Login);
            Data.Goods.GoodsModifi(_row.AssortmentId, _row.ManufacturerId, _row.CountryId, 1, _row.RetailPack, tmp, true,
                ManufactureBarCode, FixedPrice, false, _row.DefekturaAggregate, null, DateTime.Now, FirstForm.Login,
                _row.Id, _row.MD);
            ReturnGoods = tmp;

        }

        private void textBox1_Validating(object sender, CancelEventArgs e)
        {
            string str = textBox1.Text;
            if (string.IsNullOrEmpty(str))
            {
                e.Cancel = true;
                MessageBox.Show("Введите целое число");
                textBox1.SelectAll();
                return;
            }
            Int32 tmp;
            if (!Int32.TryParse(str, out tmp))
            {
                e.Cancel = true;
                MessageBox.Show("Введите целое число");
                textBox1.SelectAll();
                return;
            }
            RetPack = tmp;
            Int32 tmp2 = _row.WholesalePack;
            tmp2 = tmp2 % RetPack;
            if (tmp2 != 0)
            {
                e.Cancel = true;
                MessageBox.Show("Количество в разделяемой упаковке дочернего, должно быть кратным количеству в разделяемой родительсткого!");
                textBox1.SelectAll();
                return;
            }
        }

        private void NewGoodDiv_Load(object sender, EventArgs e)
        {
            _data = adapter.GetData(InGood);
            lblDivGoods.Text = _row.Name;
        }
    }
}